package com.sz.common.core.system.service.impl;

import com.sz.common.base.dao.ParamData;
import com.sz.common.base.service.AbstractService;
import com.sz.common.core.system.entity.SysConfig;
import com.sz.common.core.system.service.SysConfigService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Function: SysConfigServiceImpl <br>
 * Author: Charles <br>
 * Date: 2016-11-01 11:15:00
 */
@Service
public class SysConfigServiceImpl extends AbstractService implements SysConfigService {

    private static final String DEFAULT_CATALOG = "default";

    /**
     * @param key
     * @param value
     */
    @Override
    public void setProperty(String key, String value) {
        this.setProperty(1, DEFAULT_CATALOG, key, value);
    }

    /**
     * @param key          --key
     * @param defaultValue --缺省值
     * @return --value
     */
    @Override
    public String getProperty(String key, String defaultValue) {
        return getProperty(DEFAULT_CATALOG, key, defaultValue);
    }

    /**
     * @param key          --key
     * @param defaultValue --缺省值
     * @return --value
     * @Param catalog   --catalog
     */
    @Override
    public String getProperty(String catalog, String key, String defaultValue) {
        SysConfig conf = getSysConfig(catalog, key);
        if (conf != null && conf.getValue() != null && conf.getValue().length() > 0) {
            return conf.getValue();
        }
        return defaultValue;
    }

    /**
     * @param domainId --域Id
     * @param catalog  --分类
     * @param key      --key
     * @param value    --值
     */
    @Override
    public void setProperty(int domainId, String catalog, String key, String value) {
        if (key == null || key.trim().length() == 0) {
            throw new IllegalArgumentException("key cannot be empty");
        }
        if (!StringUtils.hasLength(catalog)) {
            throw new IllegalArgumentException("catalog cannot be empty");
        }
        if (domainId <= 0) {
            domainId = 1;
        }
        SysConfig conf = getSysConfig(catalog, key);
        if (conf != null) { // update
            conf.setDomainId(domainId);
            conf.setCatalog(catalog);
            conf.setValue(value);
            dao.update(this.getSqlName("updateByPrimaryKey"), conf);

        } else { // insert
            conf = new SysConfig();
            conf.setCatalog(catalog);
            conf.setKeyName(key);
            conf.setValue(value);
            conf.setDomainId(domainId);
            dao.save(this.getSqlName("insert"), conf);
        }
    }

    /**
     * @param domainId --域Id
     * @return --分类
     */
    @Override
    public List<String> getCatalog(int domainId) {
        ParamData pd = new ParamData();
        pd.put("domainId", domainId);
        return dao.findForList(this.getSqlName("selectCatalogs"), pd, String.class);
    }

    /**
     * @param domainId --域Id
     * @return --配置项
     */
    @Override
    public List<SysConfig> getProperties(int domainId) {
        return getProperties(domainId, null);
    }

    /**
     * @param domainId --域Id
     * @param catalog  --分类
     * @return --配置项
     */
    @Override
    public List<SysConfig> getProperties(int domainId, String catalog) {
        ParamData pd = new ParamData();
        pd.put("domainId", domainId);
        if (catalog != null && catalog.trim().length() > 0) {
            pd.put("catalog", catalog);
        }
        return dao.findForList(this.getSqlName("selectProperties"), pd, SysConfig.class);
    }

    /**
     * @param key --key name
     */
    @Override
    public void removeProperty(String key) {
        dao.delete(this.getSqlName("removeProperty"), key);
    }

    /**
     * @param catalog catalog
     * @param keyName --key建
     * @return
     */
    private SysConfig getSysConfig(String catalog, String keyName) {
        if (!StringUtils.hasLength(catalog) || !StringUtils.hasLength(keyName)) {
            throw new IllegalArgumentException("catalog and keyName cannot be empty");
        }
        ParamData pd = new ParamData();
        pd.put("keyName", keyName);
        pd.put("catalog", catalog);
        SysConfig conf = (SysConfig) dao.findForObject(this.getSqlName("selectProperty"), pd);
        return conf;
    }

    @Override
    protected String getMapperNamespace() {
        return "SysConfigMapper";
    }
}
